package com.bawei.module_main.dialog

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.bawei.module_main.R

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/26
 */
class MessageNoticeDialog(
    private val title: String? = null,
    private val message: SpannableStringBuilder? = null, //富文本
    private val onConfirmClick: (() -> Unit)? = null,
    private val onCancelClick: (() -> Unit)? = null,
    private val onlyConfirmButton: Boolean = false,
    private val confirmText: String = "确定",
    private val cancelText: String = "取消",
    private val cancelable: Boolean = false,
    private val alignment: Int = Gravity.CENTER,
    private val confirmClickDismiss: Boolean = true
):DialogFragment() {

    constructor(
        title: String? = null,
        message: String? = null,
        onConfirmClick: (() -> Unit)? = null,
        onCancelClick: (() -> Unit)? = null,
        onlyConfirmButton: Boolean = false,
        confirmText: String = "确定",
        cancelText: String = "取消",
        cancelable: Boolean = false,
        alignment: Int = Gravity.CENTER,
        confirmClickDismiss: Boolean = true
    ):this(
        title,
        SpannableStringBuilder(message),
        onConfirmClick,
        onCancelClick,
        onlyConfirmButton,
        confirmText,
        cancelText,
        cancelable,
        alignment,
        confirmClickDismiss
    )






    private lateinit var mParentView: View
    companion object {
        val TAG: String = MessageNoticeDialog::class.java.simpleName
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置样式
        setStyle(STYLE_NO_TITLE, com.bawei.lib_common.R.style.MessageNoticeDialog)
    }

    override fun onStart() {
        super.onStart()
        val window = dialog!!.window
        val wlp = window!!.attributes
        //屏幕
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        wlp.gravity = Gravity.CENTER
        wlp.width = (dm.widthPixels * 0.72).toInt()
        window.attributes = wlp
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mParentView = layoutInflater.inflate(R.layout.dialog_message_notice, container, false)
        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog?.setCancelable(cancelable)
        dialog?.setCanceledOnTouchOutside(cancelable)

        mParentView.findViewById<TextView>(R.id.tv_message).apply {
            text = message
            gravity = alignment
            //富文本
            highlightColor = Color.TRANSPARENT //去掉蓝色下划线
            movementMethod = LinkMovementMethod.getInstance() //点击有效果
        }

        mParentView.findViewById<TextView>(R.id.btn_confirm).text = confirmText
        mParentView.findViewById<TextView>(R.id.btn_cancel).text = cancelText

        if (title == null) {
            mParentView.findViewById<View>(R.id.tv_title).visibility = View.GONE
        } else {
            mParentView.findViewById<TextView>(R.id.tv_title).text = title
        }

        if (onlyConfirmButton) {
            mParentView.findViewById<View>(R.id.btn_cancel).visibility = View.GONE
            mParentView.findViewById<View>(R.id.line).visibility = View.GONE
        } else {
            mParentView.findViewById<View>(R.id.btn_cancel).visibility = View.VISIBLE
            mParentView.findViewById<View>(R.id.line).visibility = View.VISIBLE
            mParentView.findViewById<View>(R.id.btn_cancel).setOnClickListener {
                onCancelClick?.invoke()
                dismiss()
            }
        }
        mParentView.findViewById<View>(R.id.btn_confirm).setOnClickListener {
            onConfirmClick?.invoke()
            if (confirmClickDismiss)
                dismiss()
        }
    }
    fun show(fragmentManager: FragmentManager) {
        if (fragmentManager.findFragmentByTag(TAG)?.isVisible == true) {
            return
        }
        this.show(fragmentManager, TAG)
    }
}